import {commonApi} from "../api";

let events = []

export default {
  start(time = 10000) {
    console.log(time)
    if(this._h) {
      clearInterval(this._h)
      this._h = null
    }

    this._h = setInterval(() => {
      this._upload()
    }, time)
  },

  onEvent(name) {
    events.push({
      name,
      t: Date.now()
    })
  },
  async _upload() {
    if (events.length) {
      try {
        let tmp = events
        events = []
        await commonApi.uploadEvents(tmp)
      } catch (e) {
        console.log(e)
      }
    }
  }
}